package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.MyMovement;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;

    // 添加好友
    @Override
    public void addFriend(Long userId, Long friendId) {
        // 1.你加对方
        // 1-1 先判断friend表中是否有此记录
        boolean myExists = mongoTemplate.exists(new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);
        if (!myExists) {
            // 1-2 添加好友关系
            Friend myAddFriend = new Friend();
            myAddFriend.setCreated(System.currentTimeMillis()); // 添加时间
            myAddFriend.setUserId(userId); // 你的id
            myAddFriend.setFriendId(friendId); // 对方id
            mongoTemplate.save(myAddFriend);
            // 1-3 查询对方个人动态表
            List<MyMovement> friendMyMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + friendId);
            // 1-4 遍历
            if (CollectionUtil.isNotEmpty(friendMyMovementList)) {
                for (MyMovement friendMyMovement : friendMyMovementList) {
                    // 创建我的好友动态记录
                    FriendMovement myFriendMovement = new FriendMovement();
                    myFriendMovement.setPublishId(friendMyMovement.getPublishId()); // 好友动态id
                    myFriendMovement.setUserId(friendId); // 好友id
                    myFriendMovement.setCreated(friendMyMovement.getCreated()); // 好友动态发布时间
                    // 保存到我的好友动态表中
                    mongoTemplate.save(myFriendMovement, ConstantUtil.MOVEMENT_FRIEND + userId);
                }
            }
        }

        // 2.对方加你
        // 2-1 先判断friend表中是否有此记录
        boolean friendExists = mongoTemplate.exists(new Query(Criteria.where("userId").is(friendId).and("friendId").is(userId)), Friend.class);
        if (!friendExists) {
            // 2-2 让对方加你为好友
            Friend friendAddMy = new Friend();
            friendAddMy.setFriendId(userId);
            friendAddMy.setUserId(friendId);
            friendAddMy.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friendAddMy);
            // 2-3 查询我的个人动态表
            List<MyMovement> myMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);

            // 2-4 遍历
            if (CollectionUtil.isNotEmpty(myMovementList)) {
                for (MyMovement myMovement : myMovementList) {
                    // 创建对方的好友动态记录
                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setCreated(myMovement.getCreated()); // 我的动态发布时间
                    friendMovement.setPublishId(myMovement.getPublishId()); // 我的动态id
                    friendMovement.setUserId(userId); // 我的id
                    // 保存到对方好友动态表中
                    mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + friendId);
                }
            }
        }
    }

    // 查询好友列表
    @Override
    public PageBeanVo findFriendByPage(Long userId, Integer pageNum, Integer pageSize) {
        // 1.构建查询条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query(Criteria.where("userId").is(userId)).skip(index).limit(pageSize);

        // 2.查询list集合
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);

        // 3.查询总记录数
        long count = mongoTemplate.count(query, Friend.class);

        // 4.返回并封装分页对象
        return new PageBeanVo(pageNum, pageSize, count, friendList);
    }

    // 删除好友
    @Override
    public void removeFriend(Long userId, Long friendId) {
        // 1.你删他
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);
        // 将他的动态信息从你的好友动态表中移除
        mongoTemplate.remove(new Query(Criteria.where("userId").is(friendId)), FriendMovement.class, ConstantUtil.MOVEMENT_FRIEND + userId);
        // 2.他删你
        mongoTemplate.remove(new Query(Criteria.where("userId").is(friendId).and("friendId").is(userId)), Friend.class);
        //将你的动态从他的好友动态表中移除
        mongoTemplate.remove(new Query(Criteria.where("userId").is(userId)), FriendMovement.class, ConstantUtil.MOVEMENT_FRIEND + friendId);

    }
}
